package com.tg.app.statistics;

import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.tg.appcommon.android.TGApplicationBase;
import com.tange.module.statistics.TangeBizEventReport;

public class TangeVideoPlayStatistics {

    /** WiFi设备-实时-首帧 */
    public static final String FEATURE_WIFI_REALTIME_FIRST_FRAME = "TG_WiFi_RealTime_First_Frame_Total";
    /** WiFi设备-实时-首帧-1-点击到发起P2P */
    public static final String FEATURE_WIFI_REALTIME_FIRST_FRAME_PREPARE = "TG_WiFi_RealTime_First_Frame_Prepare";
    /** WiFi设备-实时-首帧-2-P2P连接完成 */
    public static final String FEATURE_WIFI_REALTIME_FIRST_FRAME_P2P = "TG_WiFi_RealTime_First_Frame_P2P";
    /** WiFi设备-实时-首帧-3-请求视频数据完成 */
    public static final String FEATURE_WIFI_REALTIME_FIRST_FRAME_DATA = "TG_WiFi_RealTime_First_Frame_Data";
    /** WiFi设备-实时-首帧-4-视频数据解码完成 */
    public static final String FEATURE_WIFI_REALTIME_FIRST_FRAME_DECODE = "TG_WiFi_RealTime_First_Frame_Decode";

    /** 4G设备-实时-首帧 */
    public static final String FEATURE_4G_REALTIME_FIRST_FRAME = "TG_4G_RealTime_First_Frame_Total";
    /** 4G设备-实时-首帧-1-点击到发起P2P */
    public static final String FEATURE_4G_REALTIME_FIRST_FRAME_PREPARE = "TG_4G_RealTime_First_Frame_Prepare";
    /** 4G设备-实时-首帧-2-P2P连接完成 */
    public static final String FEATURE_4G_REALTIME_FIRST_FRAME_P2P = "TG_4G_RealTime_First_Frame_P2P";
    /** 4G设备-实时-首帧-3-请求视频数据完成 */
    public static final String FEATURE_4G_REALTIME_FIRST_FRAME_DATA = "TG_4G_RealTime_First_Frame_Data";
    /** 4G设备-实时-首帧-4-视频数据解码完成 */
    public static final String FEATURE_4G_REALTIME_FIRST_FRAME_DECODE = "TG_4G_RealTime_First_Frame_Decode";


    /** WiFi设备-云回放-首帧 */
    public static final String FEATURE_WIFI_CLOUD_FIRST_FRAME = "TG_WiFi_Cloud_First_Frame_Total";

    /** WiFi设备-云回放-首帧-1-文件下载 */
    public static final String FEATURE_WIFI_CLOUD_FIRST_FRAME_DOWNLOAD = "TG_WiFi_Cloud_First_Frame_Data";

    /** WiFi设备-云回放-首帧-2-文件解密 */
    public static final String FEATURE_WIFI_CLOUD_FIRST_FRAME_DECRYPT = "TG_WiFi_Cloud_First_Frame_Decrypt";

    /** WiFi设备-云回放-首帧-3-视频解码 */
    public static final String FEATURE_WIFI_CLOUD_FIRST_FRAME_DECODE = "TG_WiFi_Cloud_First_Frame_Decode";


    /** 4G设备-云回放-首帧 */
    public static final String FEATURE_4G_CLOUD_FIRST_FRAME = "TG_4G_Cloud_First_Frame_Total";

    /** 4G设备-云回放-首帧-1-文件下载 */
    public static final String FEATURE_4G_CLOUD_FIRST_FRAME_DOWNLOAD = "TG_4G_Cloud_First_Frame_Data";

    /** 4G设备-云回放-首帧-2-文件解密 */
    public static final String FEATURE_4G_CLOUD_FIRST_FRAME_DECRYPT = "TG_4G_Cloud_First_Frame_Decrypt";

    /** 4G设备-云回放-首帧-3-视频解码 */
    public static final String FEATURE_4G_CLOUD_FIRST_FRAME_DECODE = "TG_4G_Cloud_First_Frame_Decode";


    /** WiFi设备-卡回放-首帧 */
    public static final String FEATURE_WIFI_CARD_FIRST_FRAME = "TG_WiFi_Card_First_Frame_Total";

    /** WiFi设备-卡回放-首帧-1-文件下载 */
    public static final String FEATURE_WIFI_CARD_FIRST_FRAME_DOWNLOAD = "TG_WiFi_Card_First_Frame_Data";

    /** WiFi设备-卡回放-首帧-2-文件解密 */
    public static final String FEATURE_WIFI_CARD_FIRST_FRAME_DECRYPT = "TG_WiFi_Card_First_Frame_Decrypt";

    /** WiFi设备-卡回放-首帧-3-视频解码 */
    public static final String FEATURE_WIFI_CARD_FIRST_FRAME_DECODE = "TG_WiFi_Card_First_Frame_Decode";


    /** 4G设备-卡回放-首帧 */
    public static final String FEATURE_4G_CARD_FIRST_FRAME = "TG_4G_Card_First_Frame_Total";

    /** 4G设备-卡回放-首帧-1-文件下载 */
    public static final String FEATURE_4G_CARD_FIRST_FRAME_DOWNLOAD = "TG_4G_Card_First_Frame_Data";

    /** 4G设备-卡回放-首帧-2-文件解密 */
    public static final String FEATURE_4G_CARD_FIRST_FRAME_DECRYPT = "TG_4G_Card_First_Frame_Decrypt";

    /** 4G设备-卡回放-首帧-3-视频解码 */
    public static final String FEATURE_4G_CARD_FIRST_FRAME_DECODE = "TG_4G_Card_First_Frame_Decode";


    /** 选中回放某个时间的时间点 */
    private static long startTimePlayBackTotal = 0;

    /** 回放文件开始解密的时间点 */
    private static long startTimePlayBackDecrypt = 0;

    /** 回放文件开始解码的时间点 */
    private static long startTimePlayBackDecode = 0;


    /** 点击进入视频页的时间点 */
    private static long startTimeTotal = 0;

    /** 启动P2P连接的时间点 */
    private static long startTimeP2P = 0;

    /** 启动视频数据请求的时间点 */
    private static long startTimeRequestVideoData = 0;


    /** 启动视频解码的时间点 */
    private static long startTimeDecode = 0;

    public static void resetStartTimePlayBackTotal() {
        TangeVideoPlayStatistics.startTimePlayBackTotal = System.currentTimeMillis();
    }

    public static void resetStartTimePlayBackDecrypt() {
        TangeVideoPlayStatistics.startTimePlayBackDecrypt = System.currentTimeMillis();
    }

    public static void resetStartTimePlayBackDecode() {
        TangeVideoPlayStatistics.startTimePlayBackDecode = System.currentTimeMillis();
    }

    public static void resetStartTimeTotal() {
        TangeVideoPlayStatistics.startTimeTotal = System.currentTimeMillis();
    }

    public static void resetStartTimeP2P() {
        TangeVideoPlayStatistics.startTimeP2P = System.currentTimeMillis();
    }

    public static void resetStartTimeRequestVideoData() {
        TangeVideoPlayStatistics.startTimeRequestVideoData = System.currentTimeMillis();
    }

    public static void resetStartTimeDecode() {
        TangeVideoPlayStatistics.startTimeDecode = System.currentTimeMillis();
    }

    public static void reportRealTimeFirstFrameTotal(boolean isWiFiDevice) {
        if (startTimeTotal == 0) return;
        TangeBizEventReport.reportValue(TGApplicationBase.getApplicationContext(), !isWiFiDevice ? FEATURE_4G_REALTIME_FIRST_FRAME : FEATURE_WIFI_REALTIME_FIRST_FRAME, (int) (System.currentTimeMillis() - startTimeTotal));
        startTimeTotal = 0;
    }

    public static void reportRealTimeFirstFramePrepare(boolean isWiFiDevice) {
        if (startTimeTotal == 0) return;
        TangeBizEventReport.reportValue(TGApplicationBase.getApplicationContext(), !isWiFiDevice ? FEATURE_4G_REALTIME_FIRST_FRAME_PREPARE : FEATURE_WIFI_REALTIME_FIRST_FRAME_PREPARE, (int) (System.currentTimeMillis() - startTimeTotal));
    }

    public static void reportRealTimeFirstFrameP2P(boolean isWiFiDevice) {
        if (startTimeP2P == 0) return;
        TangeBizEventReport.reportValue(TGApplicationBase.getApplicationContext(), !isWiFiDevice ? FEATURE_4G_REALTIME_FIRST_FRAME_P2P : FEATURE_WIFI_REALTIME_FIRST_FRAME_P2P, (int) (System.currentTimeMillis() - startTimeP2P));
        startTimeP2P = 0;
    }

    public static void reportRealTimeFirstFrameData(boolean isWiFiDevice) {
        if (startTimeRequestVideoData == 0) return;
        TangeBizEventReport.reportValue(TGApplicationBase.getApplicationContext(), !isWiFiDevice ? FEATURE_4G_REALTIME_FIRST_FRAME_DATA : FEATURE_WIFI_REALTIME_FIRST_FRAME_DATA, (int) (System.currentTimeMillis() - startTimeRequestVideoData));
        startTimeRequestVideoData = 0;
    }

    public static void reportRealTimeFirstFrameDecode(boolean isWiFiDevice) {
        if (startTimeDecode == 0) return;
        TangeBizEventReport.reportValue(TGApplicationBase.getApplicationContext(), !isWiFiDevice ? FEATURE_4G_REALTIME_FIRST_FRAME_DECODE : FEATURE_WIFI_REALTIME_FIRST_FRAME_DECODE, (int) (System.currentTimeMillis() - startTimeDecode));
        startTimeDecode = 0;
    }

    public static void reportPlaybackFirstFrameTotal(boolean isWiFiDevice, boolean isCloudPlayBack) {
        if (startTimePlayBackTotal == 0) return;

        if (isWiFiDevice) {
            if (isCloudPlayBack) {
                TangeBizEventReport.reportValue(TGApplicationBase.getApplicationContext(), FEATURE_WIFI_CLOUD_FIRST_FRAME, (int) (System.currentTimeMillis() - startTimePlayBackTotal));
            } else {
                TangeBizEventReport.reportValue(TGApplicationBase.getApplicationContext(), FEATURE_WIFI_CARD_FIRST_FRAME, (int) (System.currentTimeMillis() - startTimePlayBackTotal));
            }
        } else {
            if (isCloudPlayBack) {
                TangeBizEventReport.reportValue(TGApplicationBase.getApplicationContext(), FEATURE_4G_CLOUD_FIRST_FRAME, (int) (System.currentTimeMillis() - startTimePlayBackTotal));
            } else {
                TangeBizEventReport.reportValue(TGApplicationBase.getApplicationContext(), FEATURE_4G_CARD_FIRST_FRAME, (int) (System.currentTimeMillis() - startTimePlayBackTotal));
            }
        }
        startTimePlayBackTotal = 0;
    }

    public static void reportPlaybackFirstFrameDownload(boolean isWiFiDevice, boolean isCloudPlayBack) {
        if (startTimePlayBackTotal == 0) return;

        if (isWiFiDevice) {
            if (isCloudPlayBack) {
                TangeBizEventReport.reportValue(TGApplicationBase.getApplicationContext(), FEATURE_WIFI_CLOUD_FIRST_FRAME_DOWNLOAD, (int) (System.currentTimeMillis() - startTimePlayBackTotal));
            } else {
                TangeBizEventReport.reportValue(TGApplicationBase.getApplicationContext(), FEATURE_WIFI_CARD_FIRST_FRAME_DOWNLOAD, (int) (System.currentTimeMillis() - startTimePlayBackTotal));
            }
        } else {
            if (isCloudPlayBack) {
                TangeBizEventReport.reportValue(TGApplicationBase.getApplicationContext(), FEATURE_4G_CLOUD_FIRST_FRAME_DOWNLOAD, (int) (System.currentTimeMillis() - startTimePlayBackTotal));
            } else {
                TangeBizEventReport.reportValue(TGApplicationBase.getApplicationContext(), FEATURE_4G_CARD_FIRST_FRAME_DOWNLOAD, (int) (System.currentTimeMillis() - startTimePlayBackTotal));
            }
        }
    }

    public static void reportPlaybackFirstFrameDecrypt(boolean isWiFiDevice, boolean isCloudPlayBack) {
        if (startTimePlayBackDecrypt == 0) return;

        if (isWiFiDevice) {
            if (isCloudPlayBack) {
                TangeBizEventReport.reportValue(TGApplicationBase.getApplicationContext(), FEATURE_WIFI_CLOUD_FIRST_FRAME_DECRYPT, (int) (System.currentTimeMillis() - startTimePlayBackDecrypt));
            } else {
                TangeBizEventReport.reportValue(TGApplicationBase.getApplicationContext(), FEATURE_WIFI_CARD_FIRST_FRAME_DECRYPT, (int) (System.currentTimeMillis() - startTimePlayBackDecrypt));
            }
        } else {
            if (isCloudPlayBack) {
                TangeBizEventReport.reportValue(TGApplicationBase.getApplicationContext(), FEATURE_4G_CLOUD_FIRST_FRAME_DECRYPT, (int) (System.currentTimeMillis() - startTimePlayBackDecrypt));
            } else {
                TangeBizEventReport.reportValue(TGApplicationBase.getApplicationContext(), FEATURE_4G_CARD_FIRST_FRAME_DECRYPT, (int) (System.currentTimeMillis() - startTimePlayBackDecrypt));
            }
        }
        startTimePlayBackDecrypt = 0;
    }

    public static void reportPlaybackFirstFrameDecode(boolean isWiFiDevice, boolean isCloudPlayBack) {
        if (startTimePlayBackDecode == 0) return;

        if (isWiFiDevice) {
            if (isCloudPlayBack) {
                TangeBizEventReport.reportValue(TGApplicationBase.getApplicationContext(), FEATURE_WIFI_CLOUD_FIRST_FRAME_DECODE, (int) (System.currentTimeMillis() - startTimePlayBackDecode));
            } else {
                TangeBizEventReport.reportValue(TGApplicationBase.getApplicationContext(), FEATURE_WIFI_CARD_FIRST_FRAME_DECODE, (int) (System.currentTimeMillis() - startTimePlayBackDecode));
            }
        } else {
            if (isCloudPlayBack) {
                TangeBizEventReport.reportValue(TGApplicationBase.getApplicationContext(), FEATURE_4G_CLOUD_FIRST_FRAME_DECODE, (int) (System.currentTimeMillis() - startTimePlayBackDecode));
            } else {
                TangeBizEventReport.reportValue(TGApplicationBase.getApplicationContext(), FEATURE_4G_CARD_FIRST_FRAME_DECODE, (int) (System.currentTimeMillis() - startTimePlayBackDecode));
            }
        }
        startTimePlayBackDecode = 0;
    }

    /**
     * 正常打开率
     *
     * 进入页面的时候，发一个 5秒钟后上报失败的 延迟消息
     * 加载成功了，发现延迟消息没发出，就取消延迟消息，上报成功
     *
     * 退出页面要取消延迟消息
     */
    public static class SuccessfulVideoLoad {

        private static final String TAG = "SuccessfulVideoLoad";

        private static final int REPORT_FAILED_DELAY = 5000;

        private volatile static SuccessfulVideoLoad instance;

        private final Handler mainHandler;

        private boolean hasReportFailed = false;

        private SuccessfulVideoLoad() {
            mainHandler = new Handler(Looper.getMainLooper());
        }

        public static SuccessfulVideoLoad getInstance() {
            if (instance == null) {
                synchronized (SuccessfulVideoLoad.class) {
                    if (instance == null) {
                        instance = new SuccessfulVideoLoad();
                    }
                }
            }

            return instance;
        }

        public void reportFailedInTheFuture() {
            Log.i(TAG, "will reportFailed in "+ REPORT_FAILED_DELAY + " ms");
            hasReportFailed = false;
            mainHandler.removeCallbacksAndMessages(null);
            mainHandler.postDelayed(() -> {
                Log.i(TAG, "reportFailed: ");
                hasReportFailed = true;
            }, REPORT_FAILED_DELAY);
        }

        public void reportSuccessIfNeeded() {
            mainHandler.removeCallbacksAndMessages(null);
            if (hasReportFailed) {
                Log.i(TAG, "reportSuccess: NOT NEED");
            } else {
                Log.i(TAG, "reportSuccess: ");
            }
        }

        public void cancelReport() {
            Log.i(TAG, "cancelReport");
            mainHandler.removeCallbacksAndMessages(null);
            hasReportFailed = false;
        }
    }

}
